/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * CadPermissao.java
 *
 * Created on 25/11/2011, 11:08:58
 */
package visao;

import fachada.FachadaBDR;
import java.awt.Color;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import permissao.Permissao;
import util.CamposObrigatoriosException;
import util.ErroAcessoRepositorioException;

/**
 *
 * @author Administrador
 */
public class CadPermissao extends javax.swing.JFrame {

    /** Creates new form CadPermissao */
    public CadPermissao() {

        initComponents();
        this.setLocationRelativeTo(null);
        this.setIconImage(new ImageIcon(getClass().getResource("/Imagens/login.gif")).getImage());

    }

    public CadPermissao(int idPermissao) {
        initComponents();
        this.setLocationRelativeTo(null);
        this.setIconImage(new ImageIcon(getClass().getResource("/Imagens/login.gif")).getImage());
        
        try {

            FachadaBDR objfachadaBDR = FachadaBDR.getInstancia();

            Permissao objpermissao = objfachadaBDR.procurarPermissao(idPermissao);


            jTextField_idCodigo.setText(String.valueOf(objpermissao.getIdPermissao()));
            jTextField_Nome.setText(objpermissao.getDescricaoPermissao());
            jCheckBox_Status.setSelected(objpermissao.getStatusPermissao());


        } catch (CamposObrigatoriosException ex) {
            jLabel_Mensagem.setText(ex.getMessage());
            jLabel_Mensagem.setForeground(Color.red);

        } catch (Exception ex) {
            jLabel_Mensagem.setText(ex.getMessage());
            jLabel_Mensagem.setForeground(Color.red);

        }

    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel_Codigo = new javax.swing.JLabel();
        jLabel_Nome = new javax.swing.JLabel();
        jCheckBox_Status = new javax.swing.JCheckBox();
        jButton_Ok = new javax.swing.JButton();
        jButton_Cancelar = new javax.swing.JButton();
        jLabel_Mensagem = new javax.swing.JLabel();
        jTextField_Nome = new javax.swing.JTextField();
        jTextField_idCodigo = new javax.swing.JTextField();

        setTitle("Cadastro Permissâo");
        setResizable(false);

        jLabel_Codigo.setText("Codigo :");

        jLabel_Nome.setText("Nome :");

        jCheckBox_Status.setSelected(true);
        jCheckBox_Status.setText("Ativo");

        jButton_Ok.setText("Ok");
        jButton_Ok.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton_OkActionPerformed(evt);
            }
        });

        jButton_Cancelar.setText("Cancelar");
        jButton_Cancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton_CancelarActionPerformed(evt);
            }
        });

        jTextField_idCodigo.setText("0");
        jTextField_idCodigo.setEnabled(false);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(21, 21, 21)
                        .addComponent(jLabel_Codigo)
                        .addGap(18, 18, 18)
                        .addComponent(jTextField_idCodigo, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(221, 221, 221)
                        .addComponent(jCheckBox_Status))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(27, 27, 27)
                        .addComponent(jLabel_Nome)
                        .addGap(18, 18, 18)
                        .addComponent(jTextField_Nome, javax.swing.GroupLayout.PREFERRED_SIZE, 299, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(24, 24, 24)
                        .addComponent(jLabel_Mensagem, javax.swing.GroupLayout.PREFERRED_SIZE, 380, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jButton_Ok, javax.swing.GroupLayout.PREFERRED_SIZE, 86, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jButton_Cancelar, javax.swing.GroupLayout.PREFERRED_SIZE, 86, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(15, 15, 15)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(25, 25, 25)
                        .addComponent(jLabel_Codigo))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(22, 22, 22)
                        .addComponent(jTextField_idCodigo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jCheckBox_Status))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(3, 3, 3)
                        .addComponent(jLabel_Nome))
                    .addComponent(jTextField_Nome, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(20, 20, 20)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton_Ok)
                    .addComponent(jButton_Cancelar))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel_Mensagem, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton_CancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton_CancelarActionPerformed
        // TODO add your handling code here:

        int cancelar = JOptionPane.showConfirmDialog(null, "Deseja cancelar ?");
        if (cancelar == JOptionPane.YES_OPTION) {

            dispose();

        } else {
        }



    }//GEN-LAST:event_jButton_CancelarActionPerformed

    private void jButton_OkActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton_OkActionPerformed
        // TODO add your handling code here:


        int idPermissao = Integer.parseInt(jTextField_idCodigo.getText());
        String nome = jTextField_Nome.getText();
        boolean status = jCheckBox_Status.isSelected();


        try {

            FachadaBDR objFachadaBDR = FachadaBDR.getInstancia();

            Permissao permissao = new Permissao(idPermissao, nome, status);

            if (idPermissao == 0) {

                objFachadaBDR.cadastrarPermissao(permissao);

                jTextField_Nome.setText("");
                jCheckBox_Status.setSelected(false);
            } else {
                objFachadaBDR.atualizarPermissao(permissao);

            }

            String acao = (idPermissao == 0) ? "Inserido" : "Atualizado";
            jLabel_Mensagem.setText("Permissão " + acao + " Com Sucesso!!!");
            jLabel_Mensagem.setForeground(Color.BLACK);


        } catch (CamposObrigatoriosException ex) {

            jLabel_Mensagem.setText(ex.getMessage());
            jLabel_Mensagem.setForeground(Color.red);

        } catch (ErroAcessoRepositorioException ex) {

            jLabel_Mensagem.setText(ex.getMessage());
            jLabel_Mensagem.setForeground(Color.red);


        }



    }//GEN-LAST:event_jButton_OkActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new CadPermissao().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton_Cancelar;
    private javax.swing.JButton jButton_Ok;
    private javax.swing.JCheckBox jCheckBox_Status;
    private javax.swing.JLabel jLabel_Codigo;
    private javax.swing.JLabel jLabel_Mensagem;
    private javax.swing.JLabel jLabel_Nome;
    private javax.swing.JTextField jTextField_Nome;
    private javax.swing.JTextField jTextField_idCodigo;
    // End of variables declaration//GEN-END:variables
}
